package com.situ.b2c.address.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.b2c.address.dao.AddrDao;
import com.situ.b2c.address.service.AddrService;
import com.situ.b2c.pojo.Area;


@WebServlet("/AreaServlet")
public class AreaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private AddrService addrService = new AddrService();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	
	


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String parentCode = request.getParameter("parentCode");
		List<Area> areaList = addrService.selectAreas(Integer.parseInt(parentCode));
		System.out.println(areaList);
		//将数据写回到ajax请求
		//将需要写回的数据转换成json格式
		ObjectMapper mapper = new ObjectMapper();
		//将java类型转成json字符串类型
		String jsonString = mapper.writeValueAsString(areaList);
		System.out.println(jsonString);
//		mapper.writeValue(response.getOutputStream(), areaList);
		response.getWriter().println(jsonString);
	}

}
